package 队列;

public class 数组模拟队列 {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(5);
        arrayQueue.addElement(11);
        arrayQueue.addElement(12);
        arrayQueue.addElement(13);
        arrayQueue.addElement(14);
        arrayQueue.addElement(15);
//        arrayQueue.addElement(16);
        System.out.println("取出了" + arrayQueue.getElement());

        System.out.println("取出了" + arrayQueue.getElement());
        System.out.println("---");
        arrayQueue.getArrayQueue();

        System.out.println("~~~~~环形队列~~~~~~~~");
        CircleArrayQueue circleArrayQueue = new CircleArrayQueue(5);
        circleArrayQueue.addElement(111);
        circleArrayQueue.addElement(222);
        circleArrayQueue.addElement(333);
        circleArrayQueue.addElement(444);

        System.out.println("取出了" + circleArrayQueue.getElement());

        circleArrayQueue.getCircleArrayQueue();


    }
}

class ArrayQueue {
    private int front;
    private int rear;
    private int[] arr;
    private int maxiumSize;

    public ArrayQueue(int maxiumSize) {
        this.front = -1;
        this.rear = -1;
        this.maxiumSize = maxiumSize;
        this.arr = new int[maxiumSize];
    }

    public void addElement(int x) {
        if (isFull()) {
            throw new RuntimeException("队列已满，不能加数据");
        }
        arr[++rear] = x;
    }

    public int getElement() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能出数据");
        }
        return arr[++front];
    }

    public boolean isFull() {
        if (front == maxiumSize - 1) {
            return true;
        }
        return false;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public void getArrayQueue() {
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }

    }
}

class CircleArrayQueue {
    private int front;
    private int rear;
    private int[] arr;
    private int maxiumSize;

    public CircleArrayQueue(int maxiumSize) {
        this.front = 0;
        this.rear = 0;
        this.maxiumSize = maxiumSize;
        this.arr = new int[maxiumSize];
    }

    public void addElement(int x) {
        if (isFull2()) {
            System.out.println("队列已满，不能加入元素");
            return;
        }
        arr[rear] = x;
        rear = (rear + 1) % maxiumSize;
    }

    public int getElement() {
        if (isEmpty2()) {
            throw new RuntimeException("队列为空，不能获取元素");
        }
        int value = arr[front];
        front = (front + 1) % maxiumSize;
        return value;
    }

    public boolean isFull2() {
        return (rear + 1) % maxiumSize == front;
    }

    public boolean isEmpty2() {
        return front == rear;
    }

    public void getCircleArrayQueue() {
        for (int i = front; i < front + (rear + maxiumSize - front) % maxiumSize; i++) {
            System.out.printf("arr[%d]=%d\n", i % maxiumSize, arr[i % maxiumSize]);
        }
    }
}
